// © 2009 Microsoft Corporation.  All rights reserved.
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Configuration.Install;
using System.ServiceProcess;
using System.IO;
using System.Xml;
using System.Windows.Forms;

namespace Microsoft.InformationSecurity.ISRM.TaskManagment.NotificationsService
{
    /// <summary>
    /// Installer class for windows service.
    /// </summary>
    [RunInstaller(true)]
    public partial class NotificationsServiceInstaller : Installer
    {

        private ServiceInstaller serviceInstaller2;
        private ServiceProcessInstaller processInstaller;

        /// <summary>
        /// Public Constructur
        /// </summary>
        public NotificationsServiceInstaller()
        {
            InitializeComponent();
        }

        /// <summary>
        /// Invoked when installer is installing components.
        /// </summary>
        /// <param name="stateSaver">Install State</param>
        public override void Install(System.Collections.IDictionary stateSaver)
        {
            
            processInstaller = new ServiceProcessInstaller();
            serviceInstaller2 = new ServiceInstaller();

            
            processInstaller.Account = ServiceAccount.NetworkService;
            // The services are started manually.
            serviceInstaller2.StartType = ServiceStartMode.Manual;
            serviceInstaller2.ServiceName = Context.Parameters["SERVICE_NAME"];

            Installers.Add(serviceInstaller2);
            Installers.Add(processInstaller);

            base.Install(stateSaver);

            string configFilePath = Context.Parameters["INSTALLDIR"] + @"NotificationsService.exe.config";
            if (File.Exists(configFilePath)) 
            {
                XmlDocument doc = new XmlDocument();
                doc.Load(configFilePath);
                XmlNode configurationNode = doc.SelectSingleNode("/configuration/appSettings/add[@key='notificationsBuildTime']");
                configurationNode.Attributes["value"].Value = Context.Parameters["NOT_BUILD_TIME"];

                configurationNode = doc.SelectSingleNode("/configuration/appSettings/add[@key='smtpServer']");
                configurationNode.Attributes["value"].Value = Context.Parameters["SMTP_SERVER"];

                configurationNode = doc.SelectSingleNode("/configuration/appSettings/add[@key='fromEmailAddress']");
                configurationNode.Attributes["value"].Value = Context.Parameters["FROM_EMAIL"];

                configurationNode = doc.SelectSingleNode("/configuration/appSettings/add[@key='serviceName']");
                configurationNode.Attributes["value"].Value = Context.Parameters["SERVICE_NAME"];

                configurationNode = doc.SelectSingleNode("/configuration/connectionStrings/add[@name='ISRMDB']");
                configurationNode.Attributes["connectionString"].Value = Context.Parameters["DBCONN"];

                configurationNode = doc.SelectSingleNode("/configuration/loggingConfiguration/listeners/add[@name='FlatFile TraceListener']");
                configurationNode.Attributes["fileName"].Value = Context.Parameters["SERVICE_LOG_FILENAME"];
                doc.Save(configFilePath);
            }
        }
        
        /// <summary>
        /// Invoked when installer is uninstalling components.
        /// </summary>
        /// <param name="savedState">State</param>
        public override void Uninstall(System.Collections.IDictionary savedState)
        {
            try
            {
                string configFilePath = Context.Parameters["INSTALLDIR"] + @"NotificationsService.exe.config";
                if (File.Exists(configFilePath))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(configFilePath);
                    XmlNode configurationNode = doc.SelectSingleNode("/configuration/appSettings/add[@key='serviceName']");
                    processInstaller = new ServiceProcessInstaller();
                    serviceInstaller2 = new ServiceInstaller();
                    serviceInstaller2.ServiceName = configurationNode.Attributes["value"].Value;                    
                    Installers.Add(serviceInstaller2);
                    Installers.Add(processInstaller);
                }
                base.Uninstall(savedState);
            }
            catch { }
        }
    }
}
